/**
 * test case for api_user
 */
describe("api_user", function() {
	beforeEach(function() {
		App.initSession();
	});

	describe('bsoi login', function() {
		var callback = jasmine.createSpy();
		var userName = 'INUSER001', password = 'ibnk1357', cpach = null;
		beforeEach(function() {
			spyOn(bsoi, "login");
		});

		it('User can login with user name & password', function() {
			spyOn(App.api_user, "_processLogin");

			App.api_user.login(userName, password, cpach, false, callback);
			expect(App.api_user.callbackLogin).toEqual(callback);

			var t_session = App.getSession();
			expect(t_session.getBreezeContext().userContext.userId).toEqual(
					userName);
			expect(bsoi.login).toHaveBeenCalled();

			var callArgs = bsoi.login.mostRecentCall.args;
			var response = jasmine.createSpy();
			callArgs[1](response);

			expect(App.api_user._processLogin).toHaveBeenCalledWith(response);
		});

		it('Test processLogin function when user login success', function() {
			var t_response = {
				loginResponse : {
					responseHeader : {
						currentTimestamp : '2011-07-21T17:38:23.834+08:00',
						nonce : '5ce4980f999b21c2dc884b454ea562cc',
						status : 1
					},
					login : {
						customerName1 : 'XXXX',
						customerName2 : 'XXXX',
						lastLoginDate : '2011-07-21T17:33:15.902+08:00',
						securityCode : '40b4ff167767d28a908b7697f52cfc54',
						sessionId : 'Czp-8Swc1FmzuCvpgiKGVIGK7sZNNgRi'
					}
				}
			};
			
			
		});
	});

	describe('bsoi logout', function(){
		var  callback = jasmine.createSpy();
		beforeEach(function(){
			spyOn(bsoi, 'logout');
		});
		
		it('User can logout', function(){
			 var t_session = App.getSession();
			spyOn(App.api_user, '_processLogout');
			
			App.api_user.logout(callback);
			
			expect(bsoi.logout).toHaveBeenCalled();
			
			var callArgs = bsoi.logout.mostRecentCall.args;
			
			expect(callArgs[0]).toEqual({
				requestHeader : t_session.getBreezeContext()
			});
			
			var response = jasmine.createSpy();

			callArgs[1](response);

			expect(App.api_user._processLogout).toHaveBeenCalledWith(response);
			
		});
	});
	
	describe('bsoi e2elogin', function() {
		var callback = jasmine.createSpy();
		var userName = 'INUSER001', password = 'ibnk1357', cpach = null ,t_initLoginResponse, t_loginResponse;
		beforeEach(function() {
			spyOn(bsoi, "initLogin").andCallFake(function() {
				t_initLoginResponse = {
					initLoginResponse : {
						responseHeader : {
							currentTimestamp : '2011-07-21T17:38:23.834+08:00',
							nonce : '5ce4980f999b21c2dc884b454ea562cc',
							status : 1
						},
						initLogin : {
							securityParam : {
								e2eRsaExponent : '10001',
								e2eRsaModulus : '9fff14018de32cba379d80351cb1b41aa8e708efb27534be2431b78982a8d9be37f21e77c492f392753b1797418740e3044fda9b57639256f2e044e4ee8ca9e50b6799619d9c622a2be4309e89252b30b763dfad9f91372802c88b89732494376dc60b3fb398b782ab0e36ab2a6eb171c9f8ece63528f382c0022df1d22e687f'
							},
							securityNonce : 'ac7dbbc342312245dfc8bb2e9d8a',
							sessionId : 'Czp-8Swc1FmzuCvpgiKGVIGK7sZNNgRi'
						}
					}
				};
				App.api_user._processE2eLogin(t_initLoginResponse, userName, password, cpach, callback);
			});
			spyOn(bsoi, "login").andCallFake(function() {
				t_loginResponse = {
					loginResponse : {
						responseHeader : {
							currentTimestamp : '2011-07-21T17:38:23.834+08:00',
							nonce : '5ce4980f999b21c2dc884b454ea562cc',
							status : 1
						},
						login : {
							customerName1 : 'XXXX',
							customerName2 : 'XXXX',
							lastLoginDate : '2011-07-21T17:33:15.902+08:00',
							securityCode : '40b4ff167767d28a908b7697f52cfc54',
							sessionId : 'Czp-8Swc1FmzuCvpgiKGVIGK7sZNNgRi'
						}
					}
				};
				// App.api_user._processLogin(t_loginResponse, userName, password, cpach, callback);
				return t_loginResponse;
			});
		});

		it('User can e2elogin with user name & password', function() {
			spyOn(App.api_user, "_processLogin");
			
			App.api_user.login(userName, password, cpach, true, callback);
			
			expect(App.api_user.callbackLogin).toEqual(callback);
			
			expect(bsoi.initLogin).toHaveBeenCalled();
			var t_session = App.getSession();
			expect(t_session.getAttribute('e2eRsaExponent')).toEqual(t_initLoginResponse.initLoginResponse.initLogin.securityParam.e2eRsaExponent);
			expect(t_session.getAttribute('e2eRsaModulus')).toEqual(t_initLoginResponse.initLoginResponse.initLogin.securityParam.e2eRsaModulus);
			expect(t_session.getAttribute('securityNonce')).toEqual(t_initLoginResponse.initLoginResponse.initLogin.securityNonce);
			
			expect(bsoi.login).toHaveBeenCalled();
			expect(t_session.getBreezeContext().userContext.userId).toEqual(userName);
			
			var callArgs = bsoi.login.mostRecentCall.args;
			var response = jasmine.createSpy();
			callArgs[1](response);

			expect(App.api_user._processLogin).toHaveBeenCalledWith(response);
		});
	});
});